package com.tianfeng.fenghuotoutiao.ui.activity.AppListActibity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tianfeng.fenghuotoutiao.R;
import com.tianfeng.fenghuotoutiao.ui.fragment.base.BaseFragment;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class OneFragment extends BaseFragment {
   View view;
    @BindView(R.id.rel_list)
    public RecyclerView rel_list;
    AppListAdater appListAdater;
   List<AppListModel.DataBean> appLists=new ArrayList<>();
    public OneFragment() {
        // Required empty public constructor
    }


    @Override
    protected View getContentView() {
        return inflatLayout(R.layout.fragment_one);
    }

    @Override
    protected void initData() {
        super.initData();
        requestGetAppList();
    }

    /**
     * 请求小视频地址
     */
    public void requestGetAppList(){
        OkHttpClient okHttpClient=new OkHttpClient();
        final Request request = new Request.Builder()
                .url(String.format("http://tianze1688.com/api/index.php/news/user/getAppList?osversion=7.1.2&sign=d78fd1c819662088eb9f699fd266dd6d&versionCode=18&adCode=18&from_uid=5&timesc=1557217128587&phonemodel=xiaomi&phonetype=2&deviceid=864699035010366&channel=7"))
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                if (TextUtils.isEmpty(result)) return;
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    String data=jsonObject.optString("data");
                    Gson gso=new Gson();




                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            List<AppListModel.DataBean> applist=gso.fromJson(data,new TypeToken<List<AppListModel.DataBean>>(){}.getType());
                            appLists.addAll(applist);
                            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getBaseActivity());
                            rel_list.setLayoutManager(linearLayoutManager);
                            appListAdater=new AppListAdater(getActivity(),appLists);
                            rel_list.setAdapter(appListAdater);
                            appListAdater.setOnItemClickListener(new AppListAdater.OnRecyclerViewItemClickListener() {
                                @Override
                                public void onItemClick(View view, int data) {
                                    Intent intent=new Intent(getBaseActivity(),AppDetailActivity.class);
                                    Bundle mBundle = new Bundle();
                                    mBundle.putSerializable("app", appLists.get(data));
                                    intent.putExtras(mBundle);
                                    startActivity(intent);
                                }
                            });
                        }
                    });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
